package sz.szsmk.citizencard.ui.newversion.fragment.presenter

import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.model.network.bean.request.CakeCardBeanReq
import sz.szsmk.citizencard.model.network.bean.request.CheckVirCakeBeanReq
import sz.szsmk.citizencard.model.network.bean.response.*
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource
import sz.szsmk.citizencard.ui.newversion.fragment.view.PayCodeMvpView
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.AndroidUtils
import sz.szsmk.citizencard.util.StringUtils
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/10/14
 *  description :
 */
class PayCodePresenter@Inject constructor(var httpDateSourceNotUse: NetworkDateSource)
    : AppBasePresenter<PayCodeMvpView>() {

    //查询是否开通
    fun getIsOpenPayCode() {
        val checkVirCakeBeanReq = CheckVirCakeBeanReq()
        checkVirCakeBeanReq.virType = 8000
        checkVirCakeBeanReq.accessToken = ACache.get(DaggerApplication.getIns()).getAsString("access_token")

        addSubscription(HttpDateSource.createApi()?.getIsOpenPayCode(checkVirCakeBeanReq), object : ApiCallback<CheckVirCakeBeanRes?> {
            override fun onSuccess(response: CheckVirCakeBeanRes?) {
                response?.let {
                    mvpView.getIsOpenPayCodeSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })

    }

    fun queryCakeBalance(cakecode: String?) {
        val cakeCardBeanReq = CakeCardBeanReq()
        cakeCardBeanReq.card_no = cakecode

        addSubscription(HttpDateSource.createApi()?.queryCakeBalance(cakeCardBeanReq), object : ApiCallback<CakeBalanceBeanRes?> {
            override fun onSuccess(response: CakeBalanceBeanRes?) {
                response?.let {
                    mvpView.queryCakeBalanceSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })

    }

    fun getPayCode(cakecode: String?) {
        val cakeCardBeanReq = CakeCardBeanReq()
        cakeCardBeanReq.card_no = cakecode
        cakeCardBeanReq.channelId = "ZHSZ"
        cakeCardBeanReq.lat = "31.320565"
        cakeCardBeanReq.lnt = "120.676979"
        cakeCardBeanReq.count = "1"
        cakeCardBeanReq.timestamp = System.currentTimeMillis().toString()
        cakeCardBeanReq.accessToken = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        cakeCardBeanReq.deviceId = AndroidUtils.getAndroidId(DaggerApplication.getIns())

        addSubscription(HttpDateSource.createApi()?.getPayCode (cakeCardBeanReq), object : ApiCallback<CakeQRBeanRes?> {
            override fun onSuccess(response: CakeQRBeanRes?) {
                response?.let {
                    mvpView.onPayCodeSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }




}